package com.bisp30.bookinfo;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLContentHandler extends DefaultHandler {
	private List<BookInfomation> books;
	private BookInfomation bookinfo;
	private String preTag;	
	
	public List<BookInfomation> getBooks() {
		return books;
	}	
	/*
	 *
	 */
	@Override
	public void startDocument() throws SAXException {
		books = new ArrayList<BookInfomation>();
	}
	
	/*
	 * 
	 */
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		if(bookinfo!=null){
			String data = new String(ch,start,length);
			
			if("name".equals(preTag)){	
				bookinfo.setName(data);			
			}else if ("resume".equals(preTag)){
				bookinfo.setResume(data);
			}else if("bookicon".equals(preTag)){
				bookinfo.setIcon(data);
				System.out.println("Allven  bitmap 1111--------"+data);
			}else if("booksource".equals(preTag)){
				bookinfo.setSource(data);
			}
			
		}
		
	}
	/*
	 *
	 */
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if("book".equals(localName)){
			bookinfo = new BookInfomation();
			bookinfo.setId(new String(attributes.getValue("", "id")));
		}
		preTag = localName;
	}
	/*
	 *
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		if("book".equals(localName)&&bookinfo!=null){
			books.add(bookinfo);
			bookinfo = null;
		} 
		preTag = null;
	}
}